#!/bin/sh
APP_NAME='WickDD Clients'
INST_DIR=inst
DMG_DIR=$INST_DIR/dmg
C4D_DIR=$INST_DIR/c4d
C4D_PLUG_DIR=$C4D_DIR/plugins/WickDD
C4D_PREF_DIR=$C4D_DIR/prefs
DDUP_DIR="$INST_DIR/wickddup"
DDUP_APP="$DDUP_DIR/WickDD Uploader.app"
MPKG="$DMG_DIR/$APP_NAME.mpkg"
DMG_FILE=wickddcli-osx-`cat ../version.txt`.dmg

# Create installation mirror directory structure
rm -rf $INST_DIR
mkdir -p "$DMG_DIR" "$C4D_PLUG_DIR" "$C4D_PREF_DIR" "$DDUP_DIR"

echo Copying C4D plugins into installation mirror...
cp ../../default/parameters.ini "$C4D_PREF_DIR"
cp ../c4d/*.co[fh] ../version.txt ../../LICENSE "$C4D_PLUG_DIR"
cp -R ../c4d/res "$C4D_PLUG_DIR"
rm -f "$C4D_PLUG_DIR/res/*.psd"

echo Creating .app version of ddup tool in installation mirror...
py2applet ../ddup/wickddup.py icon.icns Info.plist > py2applet.log &&
		mv ../ddup/wickddup.app "$DDUP_APP"
cp ../../license.txt ../../default/parameters.ini ../version.txt \
		../ddup/*.gif ../ddup/*.png "$DDUP_APP/Contents/Resources"

echo Creating packages and metapackage...
for p in c4d wickddup
do
	/Developer/Tools/packagemaker -build -proj $p.pmproj -p $p.pkg
done
/Developer/Tools/packagemaker -build -proj wickdd-clients.pmproj -p "$MPKG"
		
if [ -d "$MPKG" ]
then
	for p in *.pkg
	do
		if [ -d "$p" ]
		then
			rm -rf "$MPKG"/Contents/Packages/$p
			cp -R "$p" "$MPKG"/Contents/Packages
		fi
	done

	echo Creating self-unboxing DMG to ship metapackage in...
	rm -f $DMG_FILE
	hdiutil create \
			-srcfolder "$DMG_DIR" \
			-volname "$APP_NAME" \
			-format UDZO -imagekey zlib-level=value \
			$DMG_FILE
	hdiutil internet-enable -yes $DMG_FILE
	hdiutil flatten $DMG_FILE

	# This is more than just cleanliness.  If you leave the .app bundle
	# in here, Installer will find it when searching for a previous
	# version of the application and will install over the top of that
	# instead of in the Applications directory.
	echo -n Removing temporary files...
	rm -rf "$INST_DIR"
	echo Done!
else
	echo Could not find metapackage!
	exit 1
fi


